/* javascript for project_edit.html */

/* 获取所有已选择用户的 id */
function get_sel_user_ids()
{
    var ids = new Array();
    for (var i = 0; i < $("#tb-users>tbody> tr").length; i++) {
        var id = $("#tb-users>tbody > tr:eq("+i+") > td:eq(4)").text();
        ids.push(id);
    }
    
    return ids;
}

function save_project()
{
    var req = new Chujue_Request();
    var proj_name = $("#txt-proj-name").val();
    var proj_desc = $("#txt-proj-desc").val();
    var proj_id = $("#hid-proj-id").val();
    var proj_userids = get_sel_user_ids();

    req.url = "/admin/proj-save";
    req.send_data = {
        "proj_name": proj_name,
        "proj_id": proj_id,
        "proj_desc": proj_desc,
        "proj_userids": proj_userids,
    };
    req.ok_func = function(data) {
        location.href = "/admin/index";
    };
    req.ajax_req();
}


function display_users()
{
    $("#user-modal ul").html(""); // 清空上次的用户
    var req = new Chujue_Request();
    req.url = "/admin/get-users";
    req.pre_check = false;
    req.is_setmessage = false;
    req.send_data = {
        "proj_userids": get_sel_user_ids(),
    };
    req.ok_func = function(data) {
        for (var i = 0; i < data.select_users.length; i++){
            var user = data.select_users[i];
            var li_html = '<li><div class="checkbox">';
            li_html += '<label><input type="checkbox">' + user["name"] + ' (真实姓名: ' + user["real_name"] + ')';
            li_html += ' <input type="hidden" value="' + user["id"] + '">';
            li_html += ' <input type="hidden" value="' + user["name"] + '">';
            li_html += ' <input type="hidden" value="' + user["real_name"] + '">';
            li_html += ' <input type="hidden" value="' + user["email"] + '">';
            li_html += '</label></div></li>';

            $("#user-modal ul").append(li_html);
        }
    };
    req.ajax_req();
}

function select_users()
{
    // 获取选中的用户
    for (var i=0; i<$("#user-modal ul > li input:checked").length; i++) {
        var chkbox = $("#user-modal ul > li input:checked").eq(i);
        var user_id = chkbox.next().val();
        var user_name = chkbox.next().next().val();
        var user_real_name = chkbox.next().next().next().val();
        var user_email = chkbox.next().next().next().next().val();

        var order = $("#tb-users > tbody > tr").length + 1;
        var tr_html = '<tr>';
        tr_html += '<td>' + order + '</td>';
        tr_html += '<td>' + user_name + '</td>';
        tr_html += '<td>' + user_real_name + '</td>';
        tr_html += '<td>' + user_email + '</td>';
        tr_html += '<td class="hidden">' + user_id + '</td>';
        tr_html += '<td><button class="btn btn-danger btn-xs" onclick="del_user(this);"><i class="fa fa-trash-o fa-fw"></i>删除</button></td>';
        tr_html += '</tr>';

        $("#tb-users > tbody").append(tr_html);
    }
    $("#user-modal").modal("hide");
}

function del_user(btn)
{
    $(btn).parent().parent().remove();
    // 重新设置用户的order, 也就是 # 下的内容
    for (var i=0; i<$("#tb-users > tbody >tr").length; i++) {
        $("#tb-users > tbody >tr:eq("+i+") td:eq(0)").text(i+1);
    }
}

function del_project()
{

    if (!confirm("确定要c删除项目吗?"))
        return;    
    
    var req = new Chujue_Request();
    req.url = "/admin/del-proj";
    req.pre_check = false;
    req.is_setmessage = false;
    req.send_data = {
        "proj_id": $("#hid-proj-id").val(),
    };
    req.ok_func = function(data) {
        location.href = "/admin/index";
    };
    req.ajax_req();
}

